<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Database\Eloquent\ModelNotFoundException;

use App\Validation\Admin\Image\Create;
use App\Validation\Admin\Image\Update;
use App\Uploader\ImageUploader;

use App\Models\Image      as mImage;
use App\Services\Image    as sImage;

use Input;

class ImageController extends BaseController
{
    protected $imageUploader;

    public function __construct(ImageUploader $imageUploader)
    {
        $this->imageUploader = $imageUploader;
    }

    /*
     * Redirect not found.
     * 错误跳转
     * @return Response
     */
    protected function redirectNotFound()
    {
        return $this->redirect('images.index')
            ->withFlashMessage('Post not found!')
            ->withFlashType('danger');
    }

    /*
     *
     * @return \Illuminate\Http\Response 
     */
    public function index()
    {
        $images = sImage::getIndexImagesList();

        return $this->view('images.index', compact('images'));
    }

    /*
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $action['target_type'] = mImage::TYPE_IMAGE_INDEX;

        return $this->view('images.create', compact('action'));
    }

    /*
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Create $request)
    {
        try {
            $data   = $request->all();

            // if(!preg_match('/'.$_SERVER['HTTP_HOST'].'/', $data['url'])){
            //     $data['url'] = 'http://'.$_SERVER['HTTP_HOST'].'/?/index-app.html'.$data['url'];
            // }

            $data['image_url']   = sImage::getImagePath($data['image_url']);
    
            if(isset($data['url'])) {
                $data['url']     = $data['url'];
            }else {
                $data['url']     = 0;
            }

            if(isset($data['item_id'])) {
                $data['item_id'] = $data['item_id'];
            }else {
                $data['item_id'] = 0;
            }

            if(isset($data['target_id'])) {
                $data['target_id'] = $data['target_id'];
            }else {
                $data['target_id'] = 0;
            }

            sImage::addNewImage($data['image_url'], $data['target_type'], $data['target_id'], $data['item_id'], $data['url']);
            
            if(!empty($data['url'])) {
                return $this->redirect('images.index');
            }elseif(!empty($data['item_id'])) {
                return $this->redirect('itemsMembers.index', array('item_id' => $data['item_id']));
            }else {
                return $this->view('404');
            }

        }catch(ModelNotFoundException $e) {
            return $this->redirectNotFound();
        }
    }

    /*
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        try{
            $image       = sImage::getImageById($id);

            return $this->view('images.edit', compact('image'));

        }catch(ModelNotFoundException $e) {
            return $this->redirectNotFound();
        }
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Update $request, $id)
    {
        try {
            $image = sImage::getImageById($id);

            $data  = $request->all();

            // if(!preg_match('/'.$_SERVER['HTTP_HOST'].'/', $data['url'])){
            //     $data['url'] = 'http://'.$_SERVER['HTTP_HOST'].'/?/index-app.html'.$data['url'];
            // }

            if(isset($data['image_url'])) {
                $data['image_url'] =  sImage::getImagePath($data['image_url']);
            }

            sImage::updateImage($image, $data);
            
            if(isset($data['url'])) {
                return $this->redirect('images.index');
            }elseif(isset($data['item_id'])) {
                return $this->redirect('itemsMembers.index', array('item_id' => $data['item_id']));
            }

        }catch(ModelNotFoundException $e) {
            return $this->redirectNotFound();
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        try {
            mImage::destroy($id);

            return $this->redirect('images.index');

        }catch(ModelNotFoundException $e) {
            return $this->redirectNotFound();
        }
    }
    
    /*
     * 图片上传
     */
    public function upload()
    {
        $this->imageUploader->upload();
    }

    /*
     * 图片预览
     */
    public function preview()
    {
        $this->imageUploader->preview();
    }
}
